package com.mujin.keji.collectionsystem.utils;


import android.util.Log;

import com.orhanobut.logger.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间格式化工具类
 *
 * @author weishixiong
 * @Time 2018-03-19
 */
public class DateUtil {
    public static final String DEFAULT_DATE_FORMATTER = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_ = "yyyy-MM-dd";
    public static final String DATE_FORMAT_MONTH = "yyyy-MM";
    public static final String DATE_FORMAT_TIME = "HH:mm:ss";
    public static final String DATE_FORMAT_HOUR_MIN = "HH:mm";
    public static final String DEFAULT_DATE_FORMATTER_MIN = "yyyy-MM-dd HH:mm";
    public static final String EPARK_DATE_FORMATER = "yyyy/MM/dd HH:mm";
    public static final String DATE_FORMAT = "yyyy/MM/dd";
    public static final String DATE_FORMAT_POINT = "yyyy.MM.dd";

    public static String formatDate(String formater, Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        return sdf.format(date);
    }

    /**
     * 传入毫秒
     *
     * @param formater
     * @param timeStamp
     * @return
     */
    public static String forMsecMatDate(String formater, long timeStamp) {
        if (timeStamp <= 0) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        return sdf.format(new Date(timeStamp));
    }

    public static String formatDate(String formater, long timeStamp) {
        if (timeStamp <= 0) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        return sdf.format(new Date(timeStamp * 1000L));
    }

    public static Date parseDate(String dateStr, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将格式化的日期转换成另一种格式
     */
    public static String formateDate(String fromDate, String pattern) {
        Date date = parseDate(fromDate, pattern);
        return formatDate(pattern, date);
    }

    /**
     * 把时间date(时间戳)转换为固定的格式destFormat
     *
     * @param destFormat String 要转换的目标格式
     * @param date       int 时间戳(单位:秒)
     * @return 转制以后的时间字符串
     */
    public static String coverDateFormat(String destFormat, int date) {
        SimpleDateFormat format = new SimpleDateFormat(destFormat);
        return format.format(date * 1000L);
    }

    public static String coverDateFormat(String destFormat, long date) {
        SimpleDateFormat format = new SimpleDateFormat(destFormat);
        return format.format(date * 1000L);
    }

    public static Date coverDateFormattoDate(String destFormat, Date date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(destFormat);
        return format.parse(format.format(date));
    }

    public static Date coverDateFormattoDate(String destFormat, int date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(destFormat);
        return format.parse(format.format(date * 1000L));
    }

    /**
     * 把时间date转换为固定的格式destFormat
     *
     * @param destFormat String 要转换的目标格式
     * @param date       date 时间
     * @return 转制以后的时间字符串
     */
    public static String coverDateFormat(String destFormat, Date date) {
        String strDate = "";
        if (date != null) {
            SimpleDateFormat format = new SimpleDateFormat(destFormat);
            strDate = format.format(date);
        }
        return strDate;
    }

    public static String formartHourAndSecond(long second) {
        long h = second / (60 * 60);
        long s = (second % (60 * 60)) / 60;
        StringBuffer sb = new StringBuffer();
        if (h > 0) {
            sb.append(h + "小时");
        }
        if (s >= 0) {
            sb.append(s + "分钟");
        }
        return sb.toString();
    }

    /**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static Date coverDateFormattoDate(String destFormat, long date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(destFormat);
        return format.parse(format.format(date));
    }

    /**
     * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
     *
     * @param date
     * @return
     */
    public static int getSeason(Date date) {
        int season = 0;
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        switch (month) {
            case Calendar.JANUARY:
            case Calendar.FEBRUARY:
            case Calendar.MARCH:
                season = 1;
                break;
            case Calendar.APRIL:
            case Calendar.MAY:
            case Calendar.JUNE:
                season = 2;
                break;
            case Calendar.JULY:
            case Calendar.AUGUST:
            case Calendar.SEPTEMBER:
                season = 3;
                break;
            case Calendar.OCTOBER:
            case Calendar.NOVEMBER:
            case Calendar.DECEMBER:
                season = 4;
                break;
        }
        return season;
    }

    //获取当天时间
    public static String getNowTime(String dateformat) {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);//可以方便地修改日期格式
        return dateFormat.format(now);
    }

    public static Date getAddMineTime(int mine) {

        Date now = new Date();
        Date afterDate = new Date(now.getTime() + mine * 60000);
        return afterDate;

    }

    public static Date getNextDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        date = calendar.getTime();
        return date;
    }

    public static Date getNextTime(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, month);
        date = calendar.getTime();
        return date;
    }

    public static String toChinese(String string) {
        String[] s1 = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String[] s2 = {"十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};

        String result = "";

        int n = string.length();
        for (int i = 0; i < n; i++) {

            int num = string.charAt(i) - '0';

            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            } else {
                result += s1[num];
            }
        }
        if (Integer.valueOf(string) < 100) {
            result = result.replace("一十", "十");
        }
        return result.replace("十零", "十");
    }


    /**
     * 格式化时间 求出 天 时 分 秒
     *
     * @param times
     */
    public static String forMat(long times) {
        long mday, mhour, mmin, msecond;//天，小时，分钟，秒
        long ss = 1000;
        long mi = ss * 60;
        long hh = mi * 60;
        long dd = hh * 24;
        mday = times / dd;
        mhour = (times - mday * dd) / hh;
        mmin = (times - mday * dd - mhour * hh) / mi;
        msecond = (times - mday * dd - mhour * hh - mmin * mi) / ss;

        String hour = null;
        String min = null;
        String second = null;
        if (String.valueOf(mhour).length() == 1) {
            hour = "0" + String.valueOf(mhour);
        } else {
            hour = String.valueOf(mhour);
        }
        if (String.valueOf(mmin).length() == 1) {
            min = "0" + String.valueOf(mmin);
        } else {
            min = String.valueOf(mmin);
        }
        if (String.valueOf(msecond).length() == 1) {
            second = "0" + String.valueOf(msecond);
        } else {
            second = String.valueOf(msecond);
        }
        return hour + ":" + min + ":" + second;
    }

    /**
     * 格式化时间 输出 年月日 小于一天 小时分钟
     *
     * @param times 单位 分钟
     */
    public static String minForDay(long times) {
        long mday, mmon, myear;//天，月，年
        long hh = 60;
        long dd = hh * 24;
        long mm = dd * 30;
        long yy = mm * 12;
        String temp = "";

        myear = times / yy;
        if (myear > 0) {
            temp += (myear + "年");
        }
        mmon = (times - myear * yy) / mm;
        if (mmon > 0) {
            temp += (mmon + "个月");
        }
        mday = (times - myear * yy - mmon * mm) / dd;
        if (mday > 0) {
            temp += (mday + "天");
        }
        if (myear == 0 && mmon == 0 && mday == 0) {
            temp = "";
            long hour = (times - myear * yy - mmon * mm - mday * dd) / hh;
            if (hour > 0) {
                temp = hour + "小时";
            }
            temp += (times - hour * 60) + "分钟";
        }
        return temp;
    }

    /**
     * @param times 时间差 ，毫秒值
     * @return 这个毫秒值对应的天数
     */
    public static String getDays(long times) {
        if (times < 0) {
            return "";
        }
        long days = times / 1000 / 60 / 60 / 24;
        return days + "";
    }

    /**
     * 取得当月天数
     */
    public static int getCurrentMonthLastDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 得到指定月的天数
     */
    public static int getMonthLastDay(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 获取当前日期是星期几index
     *
     * @param dt
     * @return 当前日期是星期几的索引
     */
    public static int getWeekOfDateIndex(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (index < 0)
            index = 0;
        return index;
    }

    /**
     * 获取某年某月第几天的date
     *
     * @param index
     * @return
     */
    public static Date getMonthIndexDate(int year, int month, int index) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, index);
        //Logger.d("", "选中时间:" + DateUtil.formatDate("yyyy-MM-dd", calendar.getTime()));
        return calendar.getTime();

    }

    /**
     * 格式化分钟
     *
     * @param
     * @return
     */
    public static String calculateWorkTime(long startWorkTime, long stopWorkTime) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        String startWorkTimeStr = DateUtil.forMsecMatDate("HH:mm", startWorkTime);
        String stopWorkTimeStr = DateUtil.forMsecMatDate("HH:mm", stopWorkTime);
        try {
            long startWorkTimeL = dateFormat.parse(startWorkTimeStr).getTime();
            long stopWorkTimeL = dateFormat.parse(stopWorkTimeStr).getTime();
            long duration = (stopWorkTimeL - startWorkTimeL) / (1000 * 60);
            return minForDay(duration);
        } catch (ParseException e) {
            e.printStackTrace();
            return "未知";
        }
    }

    /**
     * 获取本月月初时间戳
     * @return
     */
    public static long getMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();// 获取当前日期
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 0);// 设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        System.out.println(calendar.getTimeInMillis());

        return calendar.getTimeInMillis();
    }
}
